/*
	FileName : JT_C_TAX.c
	Description :
		Manage the JT_C TAX opperations.
	
	Created on 2017.12.03 by WFL.

	Revision History:
		

*/


/* Includes ------------------------------------------------------------------*/
#include "JT_C_TAX.h"
#include "GF_WFL_PI.h"
//---------------Global--------------------------------

//---------------Local--------------------------------

//----------------------------------------------------

#define JTC_39_OFFSET_TIME		3
#define JTC_39_OFFSET_SPEED		7
#define JTC_39_OFFSET_JCH		32
#define JTC_39_OFFSET_JCTYPE	34
#define JTC_39_OFFSET_DEVICE	37
#define JTC_39_OFFSET_DEVICE_b_jiangji	0x01	//1: jiangji ; 0: jiankong
#define JTC_39_OFFSET_DEVICE_b_diaoche	0x04	//1: diaoche ; 0: fei diaoche

//-------------------------------
#define JTC_F0_OFFSET_TIME		4
#define JTC_F0_OFFSET_SPEED		8
#define JTC_F0_OFFSET_DEVICE	30
#define JTC_F0_OFFSET_DEVICE_b_jiankong	0x04	//1: jiankong ; 0: jiangji
#define JTC_F0_OFFSET_DEVICE_b_Ndiaoche	0x02	//1: fei diaoche ; 0: diaoche
#define JTC_F0_OFFSET_JCTYPE	62
#define JTC_F0_OFFSET_JCH		64
//-------------------------------

//----------------------------------------------------------------
#define JTC_TAX_TYPE_NUMBER	5

#define JTC_TAX_TYPE_NO		0
#define JTC_TAX_TYPE_38		1
#define JTC_TAX_TYPE_39		2
#define JTC_TAX_TYPE_F0		3
#define JTC_TAX_TYPE_FA		4

const uint8_t JTC_TAX_TYPE_MAP[JTC_TAX_TYPE_NUMBER] = {0,0x38,0x39,0xF0,0xFA};
const uint8_t JTC_TAXFrameLen[JTC_TAX_TYPE_NUMBER] = {	0,
														TAX_FRAME_LEN_38, 
														TAX_FRAME_LEN_39, 
														TAX_FRAME_LEN_F0, 
														TAX_FRAME_LEN_FA};

//---------------------------------------------------
uint8_t JTC_TAX_getTAXtype(uint16_t head)
{
	uint8_t type;
	for(type = JTC_TAX_TYPE_NO; type < JTC_TAX_TYPE_NUMBER; type++)
	{
		if(JTC_TAX_TYPE_MAP[type] == (head & 0xFF))
			return type;
	}
	return JTC_TAX_TYPE_NO;
}
uint32_t JTC_TAX_getTAXtime(uint8_t* taxBuf)
{
	uint8_t type;
	uint32_t time = 0;
	
	type = JTC_TAX_getTAXtype(taxBuf[0]);
	if(type == JTC_TAX_TYPE_38)
	{
		type = JTC_TAX_getTAXtype(taxBuf[TAX_FRAME_LEN_38 + 0]);
		if(type == JTC_TAX_TYPE_39)
		{
			time = Mem_LE_Get32(taxBuf,TAX_FRAME_LEN_38 + JTC_39_OFFSET_TIME);
		}
	}
	else if(type == JTC_TAX_TYPE_F0 || type == JTC_TAX_TYPE_FA)
	{
		time = Mem_LE_Get32(taxBuf,JTC_F0_OFFSET_TIME);
	}
	else
	{
		time = 0;
	}
	
	return time;
}

uint32_t JTC_TAX_getTAXspeed(uint8_t* taxBuf)
{
	uint8_t type;
	uint32_t speed = 0;
	
	type = JTC_TAX_getTAXtype(taxBuf[0]);
	if(type == JTC_TAX_TYPE_38)
	{
		type = JTC_TAX_getTAXtype(taxBuf[TAX_FRAME_LEN_38 + 0]);
		if(type == JTC_TAX_TYPE_39)
		{
			speed = Mem_LE_Get32(taxBuf,TAX_FRAME_LEN_38 + JTC_39_OFFSET_SPEED) & 0xFFFFFF;
		}
	}
	else if(type == JTC_TAX_TYPE_F0 || type == JTC_TAX_TYPE_FA)
	{
		speed = Mem_LE_Get32(taxBuf,JTC_F0_OFFSET_SPEED) & 0x1FF;
	}
	else
	{
		speed = 0;
	}
	
	return speed;
}

uint32_t JTC_TAX_getTAXJCH(uint8_t* taxBuf)
{
	uint8_t type;
	uint32_t jch = 0;
	
	type = JTC_TAX_getTAXtype(taxBuf[0]);
	if(type == JTC_TAX_TYPE_38)
	{
		type = JTC_TAX_getTAXtype(taxBuf[TAX_FRAME_LEN_38 + 0]);
		if(type == JTC_TAX_TYPE_39)
		{
			jch = Mem_LE_Get32(taxBuf,TAX_FRAME_LEN_38 + JTC_39_OFFSET_JCH) & 0xFFFF;
		}
	}
	else if(type == JTC_TAX_TYPE_F0 || type == JTC_TAX_TYPE_FA)
	{
		jch = Mem_LE_Get32(taxBuf,JTC_F0_OFFSET_JCH);
	}
	else
	{
		jch = 0;
	}
	
	return jch;
}


uint16_t JTC_TAX_getTAXJCType(uint8_t* taxBuf)
{
	uint8_t type;
	uint32_t jctype = 0;
	
	type = JTC_TAX_getTAXtype(taxBuf[0]);
	if(type == JTC_TAX_TYPE_38)
	{
		type = JTC_TAX_getTAXtype(taxBuf[TAX_FRAME_LEN_38 + 0]);
		if(type == JTC_TAX_TYPE_39)
		{
			jctype = taxBuf[TAX_FRAME_LEN_38 + JTC_39_OFFSET_JCTYPE];
		}
	}
	else if(type == JTC_TAX_TYPE_F0 || type == JTC_TAX_TYPE_FA)
	{
		jctype = taxBuf[JTC_F0_OFFSET_JCTYPE];
	}
	else
	{
		jctype = 0;
	}
	
	return jctype;
}
uint8_t JTC_TAX_isDiaoche(uint8_t* taxBuf)
{
	uint8_t type;
	uint8_t diaoche = 0;
	
	type = JTC_TAX_getTAXtype(taxBuf[0]);
	if(type == JTC_TAX_TYPE_38)
	{
		type = JTC_TAX_getTAXtype(taxBuf[TAX_FRAME_LEN_38 + 0]);
		if(type == JTC_TAX_TYPE_39)
		{
			diaoche =  taxBuf[TAX_FRAME_LEN_38 + JTC_39_OFFSET_DEVICE] & \
										JTC_39_OFFSET_DEVICE_b_diaoche;
		}
	}
	else if(type == JTC_TAX_TYPE_F0 || type == JTC_TAX_TYPE_FA)
	{
		if(!taxBuf[JTC_F0_OFFSET_DEVICE] & JTC_F0_OFFSET_DEVICE_b_Ndiaoche)
			diaoche = 1;
	}
	else
	{
		diaoche = 0;
	}
	
	return diaoche;
}

uint8_t JTC_TAX_isJiangji(uint8_t* taxBuf)
{
	uint8_t type;
	uint8_t jiangji = 0;
	
	type = JTC_TAX_getTAXtype(taxBuf[0]);
	if(type == JTC_TAX_TYPE_38)
	{
		type = JTC_TAX_getTAXtype(taxBuf[TAX_FRAME_LEN_38 + 0]);
		if(type == JTC_TAX_TYPE_39)
		{
			jiangji =  taxBuf[TAX_FRAME_LEN_38 + JTC_39_OFFSET_DEVICE] & \
										JTC_39_OFFSET_DEVICE_b_jiangji;
		}
	}
	else if(type == JTC_TAX_TYPE_F0 || type == JTC_TAX_TYPE_FA)
	{
		if(!taxBuf[JTC_F0_OFFSET_DEVICE] & JTC_F0_OFFSET_DEVICE_b_jiankong)
			jiangji = 1;
	}
	else
	{
		jiangji = 0;
	}
	
	return jiangji;
}

//-----------------------------------------------------------------------






///EOF
